package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.Exception.SystemException;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.TagListDto;
import com.feng.domain.dto.TagUpdateDto;
import com.feng.domain.dto.TagsaveDto;
import com.feng.domain.entity.Tag;
import com.feng.domain.enums.AppHttpCodeEnum;
import com.feng.domain.vo.PageVo;
import com.feng.domain.vo.TagVo;
import com.feng.mapper.TagMapper;
import com.feng.service.TagService;
import com.feng.util.BeanCopyUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-10-14 17:14:38
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public ResponseResult pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        Page<Tag> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.like(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        this.page(page, queryWrapper);
        List<Tag> records = page.getRecords();

        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(records, TagVo.class);
        PageVo pageVo=new PageVo();
        //封装数据返回
        pageVo.setRows(tagVos);
        pageVo.setTotal(page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult tagSave(TagsaveDto tagsaveDto) {
        if (!StringUtils.hasText(tagsaveDto.getName())){
            //标签不能为空
            throw new SystemException(AppHttpCodeEnum.TAG_NOT_NULL);
        }
        //判断标签是否已存在
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tagsaveDto.getName());
        List<Tag> list = this.list(queryWrapper);
        if(list.size()!=0){
            //如果已存在，抛出错误信息
            throw new SystemException(AppHttpCodeEnum.TAG_IS_EXIT);
        }
        //拷贝Tag，进行保存
        Tag tag = BeanCopyUtils.copyBean(tagsaveDto, Tag.class);
         this.save(tag);
         tag.setName(tag.getName()+"("+tag.getId()+")");
         this.updateById(tag);
         return ResponseResult.okResult();

    }

    @Override
    public ResponseResult deletetag(List<Long> id) {

        //删除
        for (Long aLong : id) {
            tagMapper.deleteById(aLong);
        }


        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult getTagDetail(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);

        return ResponseResult.okResult(tagVo);

    }

    @Override
    public ResponseResult updateTag(TagUpdateDto tagupdateDto) {
        //判断修改的标签是否已存在
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tagupdateDto.getName());
        List<Tag> list = this.list(queryWrapper);
        if(list.size()!=0){
            throw new SystemException(AppHttpCodeEnum.TAG_IS_EXIT);
        }
        Tag tag = BeanCopyUtils.copyBean(tagupdateDto, Tag.class);
        this.updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTag() {
        List<Tag> list = this.list();
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }


}

